<?php
class resource_config_advertise extends resource_config_abstract {
	public static $IsLoaded = false;
	public static $_config = array();
	
	public static function setConfig($conf) {
		self::$_config = $conf;
	}
	
	public static function dir() {
		return _DIRECTORY_VAR . DIRECTORY_SEPARATOR . 'resource' . DIRECTORY_SEPARATOR . 'advertise';
	}
	
	/**
	 * 找扩展名
	 */
	public static function findExtension($code, $default = '') {
		$_ext = $default;
		$dir = self::dir() . DIRECTORY_SEPARATOR . parent::md5Path($code) . DIRECTORY_SEPARATOR;
		foreach(self::getExtensions() AS $ext) {
			if(file_exists($dir . 'original.' . $ext)) {
				$_ext = '.' . $ext;
				break;
			}
		}
		return $_ext;
	}


	public static function getExtensions($def = array('jpg')) {
		return isset(self::$_config['extensions']) ? self::$_config['extensions'] : $def;
	}
	
	public static function http($secure = true, $def = '') {
		$key = 'http';
		if($secure === true) $key = 'secure_http';
		if(!isset(self::$_config[$key])) {
			return $def;
		}
		$http = self::$_config[$key];
		$index = rand(0, count($http) - 1);
		return $http[$index];
	}
	
	public static function noMedia($def = '') {
		return isset(self::$_config['404']) ? self::$_config['404'] : $def;
	}
	
}

if(!resource_config_advertise::$IsLoaded && file_exists($filename = _DIRECTORY_SETTING . DIRECTORY_SEPARATOR . 'advertise.php')) {
	resource_config_advertise::setConfig(include_once($filename));
}
